<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="java.io.*,java.text.*,java.util.*,java.sql.*,java.text.SimpleDateFormat,java.text.DateFormat,java.util.Date,javax.servlet.*,javax.servlet.http.*,DBstep.iDBManager2000.*" %>
<%
  //自动获取OfficeServer和OCX文件完整URL路径
  String mHttpUrlName=request.getRequestURI();
  String mScriptName=request.getServletPath();
  String mServerName="OfficeServer";
  String mServerUrl="http://"+request.getServerName()+":"+request.getServerPort()+mHttpUrlName.substring(0,mHttpUrlName.lastIndexOf(mScriptName))+"/"+mServerName;//取得OfficeServer文件的完整URL
%>

<html>
<head>
<title>Word文档添加水印</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="../../js/WebOffice.js"></script>
<script type="text/javascript">
 	var WebOfficeObj = new WebOffice2015(); //创建WebOffice对象
</script>

<script language="javascript">
	  function Load()
		{
			try
			{
			  WebOfficeObj.WebUrl = "<%=mServerUrl%>";
			  WebOfficeObj.UserName = "演示人";
	      WebOfficeObj.FileName = "sample.doc";
		    WebOfficeObj.FileType = ".doc";            //FileType:文档类型  .doc  .xls
		    WebOfficeObj.DebugMode = false;						//开启or关闭调试模式  true：开启  false：关闭
		    WebOfficeObj.ShowWindow = false;					//显示/隐藏进度条
		    WebOfficeObj.EditType = "1";				//设置加载文档类型 0 锁定文档，1无痕迹模式，2带痕迹模式
		    WebOfficeObj.ShowMenu = 0;
		    WebOfficeObj.ShowToolBar = 0;
		    WebOfficeObj.SetCaption(WebOfficeObj.UserName + "正在编辑文档");
		    SetGraySkin();			//设置控件皮肤
			  if(WebOfficeObj.WebOpen())
			  {
			  	StatusMsg(WebOfficeObj.Status);
			  }
			}
			catch(e){
 	     	alert(e.description);
 	    }
		}

		//烟枪灰皮肤
 		function SetGraySkin(){
 			//参数顺序依次为：控件标题栏颜色、自定义菜单开始颜色、自定义工具栏按钮开始颜色、自定义工具栏按钮结束颜色、
 			//自定义工具栏按钮边框颜色、自定义工具栏开始颜色、控件标题栏文本颜色（默认值为：0x000000）
 			if (!WebOfficeObj.WebSetSkin(0xdbdbdb, 0xeaeaea, 0xeaeaea, 0xdbdbdb, 0xdbdbdb, 0xdbdbdb, 0x000000))
 				alert(WebOfficeObj.Status);
		}

		//插入文档水印
		function AddWaterMark(WaterMarkNmae){
			try {
  			//插入水印前需更改视图样式为页眉视图
  			WebOfficeObj.WebObject.ActiveDocument.ActiveWindow.ActivePane.View.SeekView = 9; //wdSeekCurrentPageHeader
  			//由于一个文档中只允许添加一个水印，因此在添加水印之前，需检测文档中是否存在水印，如果存在，则先删除
			AddDelWaterMark(WaterMarkNmae);

  			var selection = WebOfficeObj.WebObject.ActiveDocument.Application.Selection;
  			//设置插入水印，语法：表达式.AddTextEffect(预设文字效果【0..49】, 文字内容, 字体名, 字体大小, 是否粗体, 是否斜体, 左侧位置, 顶部位置)
  			selection.HeaderFooter.Shapes.AddTextEffect(0, '金格科技', '宋体', 36, false, false, 0, 0).Select();
  			var shapeRange = selection.ShapeRange;
  			shapeRange.Name = WaterMarkNmae;  //水印对象名
  			shapeRange.TextEffect.NormalizedHeight = false //文字效果
  			shapeRange.Line.Visible = false;					//线条是否可见
  			shapeRange.Fill.Visible = true;						//填充是否可见
  			shapeRange.Fill.Solid();									//填充类型（本例为纯色）
  			shapeRange.Fill.ForeColor.RGB = 0x0000FF; //设定填充的颜色RGB值
  			shapeRange.Fill.Transparency = 0.5;				//设置透明度50%
  			shapeRange.Rotation = 60;									//设置旋转角度
  			shapeRange.LockAspectRatio = true;				//锁定纵横比
  			//shapeRange.Height = WebOfficeObj.WebObject.ActiveDocument.Application.CentimetersToPoints(2); //高度
  			//shapeRange.Width = WebOfficeObj.WebObject.ActiveDocument.Application.CentimetersToPoints(16); //宽度
  			shapeRange.Height = 2 * 72;
  			shapeRange.Width = 8 * 72;
  			shapeRange.WrapFormat.AllowOverlap = true;	//是否允许重叠
  			shapeRange.WrapFormat.Side = 3; 		//是否设置文字环绕(wdWrapNone)
  			shapeRange.WrapFormat.type = 3;			//设置折回样式（本例设为不折回）
  			//设置水平位置与纵向页边距关联 wdRelativeVerticalPositionMargin
  			shapeRange.RelativeHorizontalPosition = 0;
  			//设置垂直位置与横向页边距关联 wdRelativeVerticalPositionMargin
  			shapeRange.RelativeVerticalPosition = 0;
  			//水平居中  wdShapeCenter
  			shapeRange.Left = -999995;
  			//垂直居中
  			shapeRange.Top = -999995;
  			//恢复视图样式到原来样式
  			WebOfficeObj.WebObject.ActiveDocument.ActiveWindow.ActivePane.View.SeekView = 0; //wdSeekMainDocument

  			StatusMsg("水印添加成功！");
  			return true;
  		}catch(e) {
  			StatusMsg("水印添加失败！");
  			return false;
  		}
		}
		//添加水印时清理存在水印
		function AddDelWaterMark(WaterMarkNmae) {
			var selection = WebOfficeObj.WebObject.ActiveDocument.Application.Selection;
			//查找文档中是否存在名称为【WaterMarkObjectName】的水印对象，如果存在，则删除
			if (selection.HeaderFooter.Shapes.Count > 0){
				for (var i = 1; i <= selection.HeaderFooter.Shapes.Count; i++){
					if (selection.HeaderFooter.Shapes.Item(i).Name = WaterMarkNmae){
						selection.HeaderFooter.Shapes.Item(i).Delete();
						break;
					}
				}
			}
		}
		//删除水印
		function DelWaterMark(WaterMarkNmae) {
			WebOfficeObj.WebObject.ActiveDocument.ActiveWindow.ActivePane.View.SeekView = 9;
			var selection = WebOfficeObj.WebObject.ActiveDocument.Application.Selection;
			//查找文档中是否存在名称为【WaterMarkObjectName】的水印对象，如果存在，则删除
			var WaterCount = selection.HeaderFooter.Shapes.Count;
			if ( WaterCount > 0){
				for (var i = WaterCount; i >= 1; i--){
					if (selection.HeaderFooter.Shapes.Item(i).Name = WaterMarkNmae){
						selection.HeaderFooter.Shapes.Item(i).Delete();
						break;
					}
				}
			}
			WebOfficeObj.WebObject.ActiveDocument.ActiveWindow.ActivePane.View.SeekView = 0;
			StatusMsg("水印删除成功！");
		}

	
		//插入图片水印
		function AddGraphicWaterMark(WaterMarkNmae) {
		try {

		WebOfficeObj.WebObject.ActiveDocument.ActiveWindow.ActivePane.View.SeekView = 9; //wdSeekCurrentPageHeader
		AddDelWaterMark(WaterMarkNmae);
		var selection = WebOfficeObj.WebObject.ActiveDocument.Application.Selection;
		/////获取服务器上的图片///////
		var ImagePath = WebOfficeObj.DownFilePath(); 
		var ImageName = "WaterMark.jpg";
		WebOfficeObj.DownloadToFile(ImageName,ImagePath);	
		////////////
		selection.HeaderFooter.Shapes.AddPicture(ImagePath + ImageName , false, true).Select(); //水印位置

		var shapeRange = selection.ShapeRange;

		shapeRange.Name = WaterMarkNmae;  //水印对象名
		shapeRange.PictureFormat.Brightness = 0.85;
		shapeRange.PictureFormat.Contrast = 0.15;
		shapeRange.LockAspectRatio = true;

		shapeRange.Height = 60.95 * 72; //高度
		shapeRange.Width = 40.63 * 72; //宽度

		shapeRange.WrapFormat.AllowOverlap = true;
		shapeRange.WrapFormat.Side = 3;//wdWrapNone
		shapeRange.WrapFormat.Type = 3;
		shapeRange.RelativeHorizontalPosition = 0;//wdRelativeVerticalPositionMargin
		shapeRange.RelativeVerticalPosition = 0;//wdRelativeVerticalPositionMargin
		shapeRange.Left = -999995;//wdShapeCenter
		shapeRange.Top = -999995;//wdShapeCenter
		WebOfficeObj.WebObject.ActiveDocument.ActiveWindow.ActivePane.View.SeekView = 0; //wdSeekMainDocument
		
		StatusMsg("水印添加成功！");
		return true;
	}catch(e) {
		StatusMsg("水印添加失败！");
		return false;
	}
	}
		

		//设置页面中的状态值
 		function StatusMsg(mValue){
 	   	try{
 	   		document.getElementById('StatusBar').value = mValue;
	   	}catch(e){
	     	return false;
	   	}
		}

		function OnUnLoad(){
			WebOfficeObj.WebClose();
		}


</script>

<!--以下是多浏览器的事件方法 -->
<script>
function OnReady(){
 WebOfficeObj.setObj(document.getElementById('WebOffice'));//给2015对象赋值
 //Load();//避免页面加载完，控件还没有加载情况
 window.onload = function(){Load();} //IE和谷歌可以直接调用Load方法，火狐要在页面加载完后去调用
}
</script>

<script language="javascript" for=WebOffice event="OnReady()">
	 WebOfficeObj.setObj(document.getElementById('WebOffice'));
   Load();//避免页面加载完，控件还没有加载情况
</script>
</head>

<body onbeforeunload="OnUnLoad()" onUnload="OnUnLoad();">
	<div style="width: 100%; height: 100%">
			<div style="width: 100%;">
			<input style="color:Red;" type=button value="添加文字水印" onclick="AddWaterMark('WaterMarkObjectName')">
			<input style="color:Red;" type=button value="添加图片水印" onclick="AddGraphicWaterMark('WaterMarkObjectName')">
			<input style="color:Red;" type=button value="删除水印" onclick="DelWaterMark('WaterMarkObjectName')">
			<input style="color:Red;" id="StatusBar" type="text" name="StatusBar" readonly  style="WIDTH:40%">&nbsp;|←状态信息
		</div>
		<div style="width: 100%; height: 98%;" ><script src="../../js/iWebOffice2015.js"></script></object></div>
	</div>
</body>
 <script language="javascript">
 var i = document.getElementById('WebOffice').height =document.documentElement.clientHeight-50 +"px";
 document.getElementById('WebOffice').style.height = i;
 window.onresize = function(){
 var i = document.getElementById('WebOffice').height =document.documentElement.clientHeight-50 +"px";
 document.getElementById('WebOffice').style.height = i;
 };
 </script>
</html>